package com.youlai.boot.system.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.annotation.RepeatSubmit;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.system.model.form.ArticleForm;
import com.youlai.boot.system.model.entity.ArticleType;
import com.youlai.boot.system.model.form.ArticleTypeForm;
import com.youlai.boot.system.model.query.ArticlePageQuery;
import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.system.model.vo.ArticlePageVO;
import com.youlai.boot.system.model.vo.ArticleVO;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 文章分类控制器
 * @author haoxr
 * @since 2020/11/6
 */
@Tag(name = "05.文章分类接口")
@RestController
@RequestMapping("/api/v1/article")
@RequiredArgsConstructor
public class ArticleController {
    private final com.youlai.boot.system.service.ArticleTypeService ArticleTypeService;
    private final com.youlai.boot.system.service.ArticleService ArticleService;
    @Operation(summary = "文章分页列表")
    @GetMapping("/page")
    @Log(value = "文章分页列表", module = LogModuleEnum.USER)
    public PageResult<ArticlePageVO> listPagedArticles(
            ArticlePageQuery queryParams
    ) {
        IPage<ArticlePageVO> result = ArticleService.listPagedArticles(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "热门文章列表")
    @GetMapping("/hotdata")
    public Result<List<ArticlePageVO>> getHotArticle(
            ArticlePageQuery queryParams
    ) {
        List<ArticlePageVO> hotArticle = ArticleService.getHotArticle(queryParams);
        return Result.success(hotArticle);
    }


    @Operation(summary = "分类热门文章列表")
    @GetMapping("/{parent_id}/indexhotdata")
    public Result<List<ArticlePageVO>> getIndexHotArticle(
            @Parameter(description ="分类ID") @PathVariable int parent_id
    ) {
        List<ArticlePageVO> flHotArticle = ArticleService.getIndexHotArticle(parent_id);
        return Result.success(flHotArticle);
    }

//    @Operation(summary = "文章分页列表")
//    @GetMapping("/jypage")
//    @Log(value = "文章分页列表", module = LogModuleEnum.USER)
//    public PageResult<ArticlePageVO> jylistPagedArticles(
//            ArticlePageQuery queryParams
//    ) {
//        IPage<ArticlePageVO> result = ArticleService.jylistPagedArticles(queryParams);
//        return PageResult.success(result);
//    }
//    @Operation(summary = "热门文章列表")
//    @GetMapping("/jyhotdata")
//    public Result<List<ArticlePageVO>> jygetHotArticle(
//    ) {
//        List<ArticlePageVO> hotArticle = ArticleService.jygetHotArticle();
//        return Result.success(hotArticle);
//    }
//
//
//
//
//    @Operation(summary = "文章分页列表")
//    @GetMapping("/jspage")
//    @Log(value = "文章分页列表", module = LogModuleEnum.USER)
//    public PageResult<ArticlePageVO> jslistPagedArticles(
//            ArticlePageQuery queryParams
//    ) {
//        IPage<ArticlePageVO> result = ArticleService.jslistPagedArticles(queryParams);
//        return PageResult.success(result);
//    }
//
//    @Operation(summary = "热门文章列表")
//    @GetMapping("/jshotdata")
//    public Result<List<ArticlePageVO>> jsgetHotArticle(
//    ) {
//        List<ArticlePageVO> hotArticle = ArticleService.jsgetHotArticle();
//        return Result.success(hotArticle);
//    }
//
//
//
//
//    @Operation(summary = "文章分页列表")
//    @GetMapping("/shpage")
//    @Log(value = "文章分页列表", module = LogModuleEnum.USER)
//    public PageResult<ArticlePageVO> shlistPagedArticles(
//            ArticlePageQuery queryParams
//    ) {
//        IPage<ArticlePageVO> result = ArticleService.shlistPagedArticles(queryParams);
//        return PageResult.success(result);
//    }
//    @Operation(summary = "热门文章列表")
//    @GetMapping("/shhotdata")
//    public Result<List<ArticlePageVO>> shgetHotArticle(
//    ) {
//        List<ArticlePageVO> hotArticle = ArticleService.shgetHotArticle();
//        return Result.success(hotArticle);
//    }


    @Operation(summary = "文章分页列表")
    @GetMapping("/list_page")
    @Log(value = "文章分页列表", module = LogModuleEnum.USER)
    public PageResult<ArticlePageVO> fllistPagedArticles(
            ArticlePageQuery queryParams
    ) {
        IPage<ArticlePageVO> result = ArticleService.fllistPagedArticles(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "文章首页列表")
    @GetMapping("/home_article_list")
    @Log(value = "文章首页列表", module = LogModuleEnum.USER)
    public Result<List<HashMap>> homeArticleList() {
        List<ArticleType> articleTypeList = ArticleTypeService.topArticleTypeList();
        List list = new ArrayList<HashMap>();
        for (ArticleType articleType : articleTypeList) {
            int articleTypeId = Math.toIntExact(articleType.getId());
            List<ArticlePageVO> zxArticles = ArticleService.getFlZxArticle(articleTypeId);
            List<ArticlePageVO> tjArticles = ArticleService.getFlTjArticle(articleTypeId);
            HashMap articleMap = new HashMap();
            articleMap.put("zxArticles", zxArticles);
            articleMap.put("tjArticles", tjArticles);
            articleMap.put("articleType", articleType);
            list.add(articleMap);
        }
        return Result.success(list);
    }



    @Operation(summary = "搜索列表")
    @GetMapping("/keywords")
    @Log(value = "文章分页列表", module = LogModuleEnum.USER)
    public PageResult<ArticlePageVO> keyWordsPagedArticles(
            ArticlePageQuery queryParams
    ) {
        IPage<ArticlePageVO> result = ArticleService.keyWordsPagedArticles(queryParams);
        return PageResult.success(result);
    }





    @Operation(summary = "推荐文章列表")
    @GetMapping("/tjdata")
    public Result<List<ArticlePageVO>> getTjArticle(
    ) {
        List<ArticlePageVO> tjArticle = ArticleService.getTjArticle();
        return Result.success(tjArticle);
    }


    @Operation(summary = "分类热门文章列表")
    @GetMapping("/{flid}/flhotdata")
    public Result<List<ArticlePageVO>> getFlHotArticle(
            @Parameter(description ="分类ID") @PathVariable int flid
    ) {
        List<ArticlePageVO> flHotArticle = ArticleService.getFlHotArticle(flid);
        return Result.success(flHotArticle);
    }


    @Operation(summary = "分类热门文章列表")
    @GetMapping("/{flid}/fltjdata")
    public Result<List<ArticlePageVO>> getFlTjArticle(
            @Parameter(description ="分类ID") @PathVariable int flid
    ) {
        List<ArticlePageVO> flTjArticle = ArticleService.getFlTjArticle(flid);
        return Result.success(flTjArticle);
    }


    @Operation(summary = "分类最新文章列表")
    @GetMapping("/{flid}/flzxdata")
    public Result<List<ArticlePageVO>> getFlZxArticle(
            @Parameter(description ="分类ID") @PathVariable int flid
    ) {
        List<ArticlePageVO> flZxArticle = ArticleService.getFlZxArticle(flid);
        return Result.success(flZxArticle);
    }



    @Operation(summary = "用户分页列表")
    @GetMapping("/{typeId}/type")
    @Log(value = "文章分页列表", module = LogModuleEnum.USER)
    public PageResult<ArticlePageVO> listTypePagedArticles(
            ArticlePageQuery queryParams
    ) {
        IPage<ArticlePageVO> result = ArticleService.listPagedArticles(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增文章")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:article:add')")
    @RepeatSubmit
    public Result<?> saveArticle(
            @RequestBody @Valid ArticleForm articleForm
    ) {
        boolean result = ArticleService.saveArticle(articleForm);
        return Result.judge(result);
    }

    @Operation(summary = "获取相关文章数据")
    @GetMapping("/{articleId}/relate_article")
    public Result<List<ArticlePageVO>> getRelateArticle(
            @Parameter(description ="文章ID") @PathVariable Long articleId
    ) {
        ArticleVO ArticleForm = ArticleService.getArticleForm(articleId);
        int flid = ArticleForm.getFlid();
        List<ArticlePageVO> relateArticle = ArticleService.getRelateArticle(articleId,flid);
        return Result.success(relateArticle);
    }

    @Operation(summary = "获取文章表单数据")
    @GetMapping("/{articleId}/form")
    public Result<ArticleVO> getArticleForm(
            @Parameter(description ="文章ID") @PathVariable Long articleId
    ) {
        ArticleVO ArticleForm = ArticleService.getArticleForm(articleId);
        long flid = ArticleForm.getFlid();
        ArticleTypeForm ArticleTypeForm =ArticleTypeService.getArticleTypeForm(flid);
        long parent_id = ArticleTypeForm.getParentId();
        ArticleTypeForm ArticleTypeParentForm =ArticleTypeService.getArticleTypeForm(parent_id);
        ArticleForm.setTypeTopName(ArticleTypeParentForm.getName());
        return Result.success(ArticleForm);
    }


    @Operation(summary = "获取文章上一页和下一页")
    @GetMapping("/{articleId}/getprevnextdata")
    public Result<HashMap> getPrevNextData(
            @Parameter(description ="文章ID") @PathVariable Long articleId
    ) {
        ArticleVO ArticleForm = ArticleService.getArticleForm(articleId);
        int flid = ArticleForm.getFlid();
        ArticleVO ArticlePrevData = ArticleService.getArticlePrevData(articleId,flid);
        ArticleVO ArticleNextData = ArticleService.getArticleNextData(articleId,flid);
        HashMap map = new HashMap();
        map.put("prev_data", ArticlePrevData);
        map.put("next_data", ArticleNextData);
        return Result.success(map);
    }


    @Operation(summary = "修改文章")
    @PutMapping(value = "/{articleId}")
//    @PreAuthorize("@ss.hasPerm('sys:article:edit')")
    public Result<?> updateArticle(
            @Parameter(description = "用户ID") @PathVariable Long articleId,
            @RequestBody @Validated ArticleForm articleForm) {
        boolean result = ArticleService.updateArticle(articleId, articleForm);
        return Result.judge(result);
    }


    @Operation(summary = "浏览文章")
    @GetMapping("/{articleId}/view")
    public Result<Void> viewArticle(
            @Parameter(description = "文章ID") @PathVariable Long articleId
    ) {
        boolean result = ArticleService.viewArticle(articleId);
        return Result.judge(result);
    }



    @Operation(summary = "删除文章")
    @DeleteMapping("/{ids}")
//    @PreAuthorize("@ss.hasPerm('sys:user:delete')")
    public Result<Void> deleteArticles(
            @Parameter(description = "用户ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = ArticleService.deleteArticles(ids);
        return Result.judge(result);
    }


}